package huixue.cn.com.rxapp.stock.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v4.graphics.ColorUtils;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Arrays;

import huixue.cn.com.library.util.LogUtils;
import huixue.cn.com.library.util.PixelUtils;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.stock.adapter.CommonAdapter;

/**
 * Created by gongdongyang on 2018/8/16.
 */

public class HRecycleView extends RelativeLayout {

    //头部title布局
    private LinearLayout mRightTitleLayout;

    //左边标题集合
    private CharSequence[] mLeftTextList;
    //右边标题集合
    private CharSequence[] mRightTitleList = new String[]{};

    private Context mContext;

    private RecyclerView mRecyclerView;

    //RecycleView的Adapter
    private CommonAdapter mAdapter;

    //触发拦截手势的最小值
    private int mTriggerMoveDis=30;

    //需要滑动的View集合
    private ArrayList<View> mMoveViewList = new ArrayList();

    private float mStratX;

    public HRecycleView(Context context) {
        super(context);
        mContext = context;
    }

    public HRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.HRecycleView);
        mLeftTextList = typedArray.getTextArray(R.styleable.HRecycleView_leftTitle);
        mRightTitleList = typedArray.getTextArray(R.styleable.HRecycleView_rightTitle);
        init();
    }

    private void init(){
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(createHeadLayout());
        linearLayout.addView(createMoveRecyclerView());

        addView(linearLayout);
    }

    public View createHeadLayout(){
        LinearLayout headLayout = new LinearLayout(mContext);
        headLayout.setBackgroundColor(Color.parseColor("#eeeeee"));
        headLayout.setOrientation(LinearLayout.HORIZONTAL);
        headLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        //左边title
        LinearLayout leftLayout = new LinearLayout(mContext);
        leftLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        leftLayout.setOrientation(LinearLayout.HORIZONTAL);
        addHeadTitle(leftLayout,mLeftTextList);
        headLayout.addView(leftLayout,0);

        //右边title
        mRightTitleLayout = new LinearLayout(mContext);
        mRightTitleLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mRightTitleLayout.setOrientation(LinearLayout.HORIZONTAL);
        addHeadTitle(mRightTitleLayout,mRightTitleList);
        headLayout.addView(mRightTitleLayout,1);
        return headLayout;

    }

    private RecyclerView createMoveRecyclerView(){
        mRecyclerView = new RecyclerView(getContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        return mRecyclerView;
    }

    private void addHeadTitle(ViewGroup parent, CharSequence[] title){
        for(int i=0;i<title.length;i++){
            TextView textView = new TextView(mContext);
            textView.setGravity(Gravity.CENTER);
            textView.setText(title[i]);
            textView.setTextColor(Color.parseColor("#000000"));
            parent.addView(textView,new LinearLayout.LayoutParams(PixelUtils.dp2px(60),PixelUtils.dp2px(50)));
        }
    }

    public void setAdapter(RecyclerView.Adapter adapter){
        mAdapter = (CommonAdapter) adapter;
        if(null !=mAdapter){
            if (mAdapter instanceof CommonAdapter) {
                mRecyclerView.setAdapter((CommonAdapter) mAdapter);
                mMoveViewList = ((CommonAdapter) mAdapter).getMoveViewList();
            }
        }

    }

    /**
     * getX--相对容器的坐标位置
     * RawX,RawY 相对于屏幕位置坐标
     * @param ev
     * @return
     *
     * 这两者的介绍只有一字之差，一个是“move”，一个是“set”，因此，我的理解是scrollTo是相对于view的初始位置的，是不会变的。而scrollBy是相对于当前位置的，位置一直是移动的。
     * scrollBy 调用 scrollTo
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mStratX = ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                float delX = ev.getX()-mStratX;
                if(Math.abs(delX)>mTriggerMoveDis){
                    return true;
                }else {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
    float mFixX = 0;
    float mMoveOffsetX = 0;
    float delX = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                delX = event.getX()-mStratX;
                if(Math.abs(delX)>mTriggerMoveDis){
                    mMoveOffsetX = (int) (mStratX - event.getX() + mFixX);
                    if (0 > mMoveOffsetX) {
                        mMoveOffsetX = 0;
                    }else if(mRightTitleLayout.getWidth()+mMoveOffsetX>PixelUtils.dp2px(mRightTitleList.length*60)){
                        mMoveOffsetX = PixelUtils.dp2px(mRightTitleList.length*60) - mRightTitleLayout.getWidth();
                    }
                    mRightTitleLayout.scrollTo((int)(mMoveOffsetX),0);
                    if (null != mMoveViewList) {
                        for (int i = 0; i < mMoveViewList.size(); i++) {
                            //使每个item随着手指向右滚动
                            mMoveViewList.get(i).scrollTo((int)mMoveOffsetX, 0);
                        }
                    }

                }
                break;
            case MotionEvent.ACTION_UP:
                mFixX = mMoveOffsetX;
                break;
        }
        return super.onTouchEvent(event);

    }
}
